package cn.herlioner.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.junit.Test;

import cn.herlioner.dao.impl.Obj_TeaDAOImpl;
import cn.herlioner.dao.impl.ObjectDAOImpl;
import cn.herlioner.pojo.CRUObject;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.service.CRUObjectService;
import cn.herlioner.service.PageService;

import com.opensymphony.xwork2.ActionContext;

public class Clg{
	

	public static void main(String[] args) {   
		CRUObject cruObject; 
		List<CRUObject> cruObjects = new ArrayList();
		CRUObjectService cruObjectService = new CRUObjectService();
		ObjectDAOImpl objectDAO = new ObjectDAOImpl();
		
		ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 200, TimeUnit.MILLISECONDS,
	                 													new LinkedBlockingQueue<Runnable>(5));
		long startTime = System.currentTimeMillis();
	         PageService Pageservice = objectDAO.PageSelect(1, 7);
	         
	         for(int i=0;i<8;i++){
	        	 MyTask myTask = new MyTask(i,Pageservice,cruObjects,cruObjectService);
	             executor.execute(myTask);
	             System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
	             executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());
	         }
	        
	         System.out.println(Pageservice);
	         
	         executor.shutdown();
	         System.out.println(cruObjects.toString());
	         
	     }
		}
	
	 
	class MyTask implements Runnable {
	    private int taskNum;
	    private PageService Pageservice ;
	    private CRUObjectService cruObjectService;
	    private List<CRUObject> cruObjects;
	    
	    public MyTask(int num ,PageService Pageservice ,List<CRUObject> cruObjects,CRUObjectService cruObjectService) {
	        this.taskNum = num;
	        this.cruObjects = cruObjects;
	        this.Pageservice = Pageservice;
	        this.cruObjectService = cruObjectService;
	    }
	     
	    @Override
	    public void run() {
	        System.out.println("正在执行task "+taskNum);
	        try {
	        	//每次执行一次查询
		    	CRUObject cruObject = new CRUObject();
			    TSObject obj = (TSObject)Pageservice.getRecordList().get(taskNum);
			    cruObject = cruObjectService.setobject(obj,cruObject);
			    System.out.println(cruObject);
			    cruObjects.add(cruObject);
			    System.out.println(cruObjects);
	        } catch (Exception e) {
	          System.out.println("数组超界");
	        }
	        System.out.println("task "+taskNum+"执行完毕");
	    }
	}
